package com.cloud.ui;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;


import com.cloud.bean.FileItem;
import com.cloud.control.FileItemAdapter;
import com.filebrowsing.net.micode.fileexplorer.FileExplorerTabActivity;
import com.mogujie.tt.R;

public class FilesFragment extends Fragment  {
	private ListView lv_files;
	private LinearLayout mDownloadLayout;
	private LinearLayout mUploadLayout;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_files, container, false);
		setHasOptionsMenu(true);//设置菜单上下文
		findView(view);
		init();
		return view;
	}

	private void findView(View v) {
		lv_files = (ListView) v.findViewById(R.id.lv_files);
		mDownloadLayout =(LinearLayout)v.findViewById(R.id.download_layout);
		mUploadLayout = (LinearLayout)v.findViewById(R.id.upload_layout);
	}

	private void init() {
		List<FileItem> files = new ArrayList<FileItem>();
		files.add(new FileItem(R.drawable.folder, "音乐", ""));
		files.add(new FileItem(R.drawable.apk, "Baidu.apk", "18.3K 修改时间:2012-12-08"));
		files.add(new FileItem(R.drawable.avi, "哈利 波特.avi", "28.3M 修改时间:2013-09-18"));
		/*files.add(new FileItem(R.drawable.epub, "不能说的秘密.epub", "18.3B 修改时间:2012-12-08"));
		files.add(new FileItem(R.drawable.mkv, "钢铁侠.mkv", "18.3K 修改时间:2012-12-08"));
		files.add(new FileItem(R.drawable.pdf, "欢迎.pdf", "183.3K 修改时间:2012-12-08"));
		files.add(new FileItem(R.drawable.zip, "游戏.zip", "13.3B 修改时间:2012-04-08"));
		files.add(new FileItem(R.drawable.rar, "集合.rar", "8.3G 修改时间:2012-12-08"));
		files.add(new FileItem(R.drawable.txt, "三国.txt", "18.3K 修改时间:2012-12-08"));*/


		FileItemAdapter adapter = new FileItemAdapter(getActivity(), files);
		lv_files.setOnItemClickListener(adapter);
	//	lv_files.setOnItemLongClickListener(adapter);
		lv_files.setAdapter(adapter);

		mDownloadLayout.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				int count = 0;

				List<TextView> textViews = new ArrayList<TextView>();
				for (int i = 0; i < lv_files.getChildCount(); i++) {
					CheckBox checkBox = (CheckBox) lv_files.getChildAt(i).findViewById(R.id.icheckbox_file_operate);
					if (checkBox.isChecked()) {
						textViews.add((TextView) lv_files.getChildAt(i).findViewById(R.id.tv_file_name));
						count++;
					}
				}
				Toast.makeText(getActivity(), "有" + count + "文件被选中", Toast.LENGTH_SHORT).show();
				for (TextView textView : textViews) {
					Toast.makeText(getActivity(), "其中有" + textView.getText().toString() + "文件被选中", Toast.LENGTH_SHORT).show();
				}
			}
		});

		mUploadLayout.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				startActivity(new Intent(getActivity(), FileExplorerTabActivity.class));
			}
		});

		registerForContextMenu(lv_files);//注册listview上下文

	}


	@Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
	//	super.onCreateContextMenu(menu, v, menuInfo);
		// set context menu title
		//menu.setHeaderTitle("文件操作");
		// add context menu item
		menu.add(0, 1, Menu.NONE, "打开");
		menu.add(0, 2, Menu.NONE, "下载");
		menu.add(0, 3, Menu.NONE, "重命名");
		menu.add(0, 4, Menu.NONE, "删除");
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		// 得到当前被选中的item信息
		AdapterView.AdapterContextMenuInfo info=(AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
		int index=info.position;
		TextView textView = (TextView)lv_files.getChildAt(index).findViewById(R.id.tv_file_name);
		//Log.v("FilesFragment", "context item index="+ index);
		switch(item.getItemId()) {
			case 1:
				Toast.makeText(getActivity(),"打开"+textView.getText().toString(),Toast.LENGTH_SHORT).show();
			break;
			case 2:
				Toast.makeText(getActivity(),"下载"+textView.getText().toString(),Toast.LENGTH_SHORT).show();
			break;
			case 3:
				Toast.makeText(getActivity(),"重命名"+textView.getText().toString(),Toast.LENGTH_SHORT).show();
			break;
			case 4:
				Toast.makeText(getActivity(),"删除"+textView.getText().toString(),Toast.LENGTH_SHORT).show();
			break;
			default:
				return super.onContextItemSelected(item);
		}
		return true;
	}

}